#include "RealMain.h"
#include "lhcutils/StartupEnvironment.h"
#include "PatcherOptions.h"
#include "lhcutils/Exceptions.h"
#include "ExitCodes.h"
#include "lhcutils/Console.h"
#include "Patcher.h"
#include "lhcutils/Format.h"
#include "lhcutils/i18n.h"
#include "FormatNumSeconds.h"
#include <boost/logic/tribool.hpp>

using namespace lhcutils;

namespace itgoggpatch
{

bool UserWantsToContinue(const PatcherOptions& options);
boost::tribool ResponseIsAffirmative(const UnicodeString& response);

int RealMain(const StartupEnvironment& argsAndStuff)
{
	try
	{
		PatcherOptions options(argsAndStuff);
		if(options.ShouldDisplayHelp())
		{
			options.PrintVersion(Console);
			options.PrintHelp(Console);
			return EXITCODE_SUCCESS;
		}
		else if(options.ShouldDisplayVersion())
		{
			options.PrintVersion(Console);
			return EXITCODE_SUCCESS;
		}
		
		bool userWantsToContinue = UserWantsToContinue(options);
		if(userWantsToContinue)
		{
			Patcher patcher(options);
			bool patcherCompletedWithoutErrors = patcher.Patch();
			if(options.IsInteractive())
			{
				if(patcherCompletedWithoutErrors)
				{
					Console.WriteLine("Done. Press enter to exit.");
				}
				else
				{
					Console.WriteLine("Press enter to exit.");
				}

				Console.ReadLine();
			}
		}
	}
	catch(const CommandLineOptionException& ex)
	{
		Console.WriteLine(*ex.UnicodeWhat());
		Console.WriteLine("For help on command line options, run {0} --help", *argsAndStuff.GetInvocationName());
		return EXITCODE_ERROR;
	}
	catch(const Exception& ex)
	{
		Console.WriteLine("Unexpected error: {0} {1}", ex.UnicodeWhat(), GetBugReportString(ex));
		return EXITCODE_ERROR;
	}
	catch(const std::exception& ex)
	{
		Console.WriteLine("Unexpected error: {0} This is a bug, please report it.", ex.what());
		return EXITCODE_ERROR;
	}
	catch(...)
	{
		Console.WriteLine("Unexpected unknown error. Something must have really went wrong! This is a bug, please report it.");
		return EXITCODE_ERROR;
	}
}

bool UserWantsToContinue(const PatcherOptions& options)
{
	if(!options.IsInteractive())
	{
		return true;
	}

	if(options.PatchingToRealLength())
	{
		StringPtr message = format("You have chosen to patch the following files and directories to the song's true length: {0}",
			*FormatList(options.GetStartingPaths()));
		Console.WriteLine(*message);
	}
	else
	{
		StringPtr message = format("You have chosen to patch the following files and directories to {0}: {1}",
			FormatNumSeconds(options.GetTimeInSecondsToPatchTo()),
			FormatList(options.GetStartingPaths()));
		Console.WriteLine(*message);
	}

	boost::tribool userWantsToProceed;
	
	do
	{
		Console.WriteLine("Proceed? (y/n)");
		StringPtr response = Console.ReadLine();
		userWantsToProceed = ResponseIsAffirmative(*response);
	} while(boost::logic::indeterminate(userWantsToProceed));

	return userWantsToProceed;
}

StringPtr FormatNumSeconds(double numSeconds)
{
	return format("{0} seconds", UnicodeString(numSeconds, CultureInfo::GetCurrentCulture()));
}

boost::tribool ResponseIsAffirmative(const UnicodeString& response)
{
	if(response.StartsWith("y", stringcomparison::CurrentCultureIgnoreCase))
	{
		return true;
	}
	else if(response.StartsWith("n", stringcomparison::CurrentCultureIgnoreCase))
	{
		return false;
	}
	else
	{
		return boost::tribool(boost::logic::indeterminate);
	}
}

}

/*
 Copyright 2011 Greg Najda

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
